{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task \n",
    "\n",
    "This notebooks merges HRS and SHARE for estimation of moments. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np \n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "country\n",
       "10    17823\n",
       "11     1893\n",
       "12     3007\n",
       "13     3053\n",
       "14     2972\n",
       "15     2396\n",
       "16     2559\n",
       "17     3193\n",
       "18     1707\n",
       "19     2898\n",
       "20     1003\n",
       "23     3827\n",
       "25     2595\n",
       "Name: rage_w1, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hrs = pd.read_pickle('../data_sources/hrs/rndhrs_wide_select.pkl')\n",
    "sh = pd.read_pickle('../data_sources/share/share_wide_select.pkl')\n",
    "df = hrs.append(sh)\n",
    "df.groupby('country').count()['rage_w1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "country\n",
       "10    17823\n",
       "12     3007\n",
       "13     3053\n",
       "14     2972\n",
       "15     2396\n",
       "16     2559\n",
       "17     3193\n",
       "18     1707\n",
       "Name: rage_w1, dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hrs = pd.read_pickle('../data_sources/hrs/rndhrs_wide_select.pkl')\n",
    "sh = pd.read_pickle('../data_sources/share/share_wide_select.pkl')\n",
    "df = hrs.append(sh)\n",
    "df.groupby('country').count()['rage_w1']\n",
    "cnums = [10,12,13,14,15,16,17,18]\n",
    "df = df[df.country.isin(cnums)]\n",
    "df.groupby('country').count()['rage_w1']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We then select the countries used in the paper. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "country\n",
       "10    17823\n",
       "12     3007\n",
       "13     3053\n",
       "14     2972\n",
       "15     2396\n",
       "16     2559\n",
       "17     3193\n",
       "18     1707\n",
       "Name: rage_w1, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnums = [10,12,13,14,15,16,17,18]\n",
    "df = df[df.country.isin(cnums)]\n",
    "df.groupby('country').count()['rage_w1']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We select the age range. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df[(df['rage_w1']>=50) & (df['rage_w1']<=75)]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We create the mapping to country numbers from 1 to 8 that we use in paper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['cid'] = df['country'].replace({12:1,13:2,14:3,15:4,16:5,17:6,18:7,10:8})\n",
    "cnames = ['DE','SE','NL','SP','IT','FR','DK','US']\n",
    "cids = [x for x in range(1,9)]\n",
    "map_cnames = dict(zip(cids,cnames))\n",
    "df['cname'] = df['cid'].replace(map_cnames)\n",
    "df.groupby('cname').count()['rage_w1']\n",
    "df['share'] = np.where(df['cid']!=8,1,0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We compute the transition variables, whether in good health in wave 2 dependent on whether the respondent is in poor or good health in wave 1. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_adla = {0:0,1:1,2:1,3:1,4:1,5:1}\n",
    "df['radla_w1'] = df['radla_w1'].replace(map_adla)\n",
    "df['radla_w2'] = df['radla_w2'].replace(map_adla)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['gg2'] = np.where(df['radla_w1']==0,1-df.loc[:,'radla_w2'],np.nan)\n",
    "df['gb2'] = np.where(df['radla_w1']==1,1-df.loc[:,'radla_w2'],np.nan)\n",
    "df['g_w1'] = 1-df.loc[:,'radla_w1']\n",
    "df['g_w2'] = 1-df.loc[:,'radla_w2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_pickle('../data_sources/hrs-share_wide_select.pkl')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "cf2a50979671a58939829e6829efb726aa5da11149213b77bd50351f899d04fb"
  },
  "kernelspec": {
   "display_name": "Python 3.8.5 64-bit ('base': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
